#include "animationstackedwidget.h"

animationStackedWidget::animationStackedWidget(QWidget *parent)
    :QStackedWidget(parent)

{
    m_nextPageIndex=0;
}

animationStackedWidget::~animationStackedWidget()
{

}

void animationStackedWidget::animation(int pageIndex)
{
    m_nextPageIndex=pageIndex;
    QPropertyAnimation *animation=new QPropertyAnimation(this,"rotateVal");
    //设置动画持续时间
    animation->setDuration(700);
    animation->setEasingCurve(QEasingCurve::Linear);//动画缓和曲线
    //设置动画起始值
    animation->setStartValue(m_startVal);
    //设置动画结束值
    animation->setEndValue(m_endVal);

    connect(animation,&QPropertyAnimation::valueChanged,this,&animationStackedWidget::OnValuechanged);
    connect(animation,&QPropertyAnimation::finished,this,&animationStackedWidget::OnFinished);
    //设置动画开始
    currentWidget()->hide();
    animation->start();

}

void animationStackedWidget::OnValuechanged(const QVariant &value)
{
    //iRotateVal = value.toFloat();
    qDebug() << "Rotation value:" << iRotateVal; // 调试输出
    repaint();
}

void animationStackedWidget::OnFinished()
{
    //动画结束
    widget(m_nextPageIndex)->show();//旋转结束显示第二页
    widget(m_nextPageIndex)->raise();
    setCurrentWidget(widget(m_nextPageIndex));

}

void animationStackedWidget::paintEvent(QPaintEvent *ev)
{
    QPainter p(this);//窗口作为画板


    if(iRotateVal>0&&iRotateVal<=90)
    {//旋转
        QPixmap s("://image/1.png");
        QPixmap pix(currentWidget()->size());
        currentWidget()->render(&pix);//只会绘制当前页图片的所在位置
        QTransform transform;
        transform.translate(width()/2,0);//图片移动到中点
        transform.rotate(iRotateVal, Qt::YAxis);
        p.setTransform(transform);
        p.drawPixmap(-1*width()/2, 0,pix);//换顶点绘制
    }
    else if(iRotateVal>90&&iRotateVal<=180)
    {       QPixmap s("://image/2.png");
        QPixmap pix(widget(m_nextPageIndex)->size());
        widget(m_nextPageIndex)->render(&pix);
        QTransform transform;
        transform.translate(width()/2,0);
        transform.rotate(iRotateVal+180, Qt::YAxis);
        p.setTransform(transform);
        p.drawPixmap(-1*width()/2, 0,pix);
    }


}
